const { object } = require('joi')
const db = require('../db/index')
const fs = require('fs')

//获取二级学院的名称
exports.get_collegeName = function (req, res) {
    db.query('select college_name from college', function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 0) return res.cc('没有二级学院的信息')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//获取学院管理员个人信息
exports.get_bigAdminInfo = function (req, res) {
    // console.log(req.user);
    db.query('select * from  bigadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '你的信息来了',
            data: results
        })
    })
}

//获取所有学生信息
exports.get_studentAllInfo = function (req, res) {
    // console.log(req.query);
    // 都不存在
    if (!req.query.region && !req.query.user) {
        db.query('select * from student order by student_num', function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有查到学生信息')
            // console.log(results);
            const total = results.length
            const pageSize = req.query.limit
            const pageCurrent = req.query.current
            const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
            // console.log(datalist);
            return res.send({
                status: 0,
                message: '获取学生信息成功',
                data: { datalist, total }
            })
        })
    }
    // 学号不存在，学院存在
    if (!req.query.user && req.query.region) {
        db.query('select * from student where student_college=? order by student_num', req.query.region, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('没有当前学院的人')
            // console.log(result);
            const total = result.length
            const pageSize = req.query.limit
            const pageCurrent = req.query.current
            const datalist = result.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
            // console.log(datalist);
            return res.send({
                status: 0,
                message: "获取成功",
                data: { datalist, total }
            })
        })
    }
    // 学号存在，学院不存在
    if (req.query.user && !req.query.region) {
        db.query('select * from student  where student_num=? order by student_num', req.query.user, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('该学号的学生不存在')

            const total = results.length
            const pageSize = req.query.limit
            const pageCurrent = req.query.current
            const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
            return res.send({
                status: 0,
                message: "获取成功",
                data: { datalist, total }
            })
        })
    }
    // 学号存在，学院存在
    if (req.query.user && req.query.region) {
        db.query('select * from student  where student_num=? and student_college=? order by student_num', [req.query.user, req.query.region], function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有查到相应的数据')
            const total = results.length
            const pageSize = req.query.limit
            const pageCurrent = req.query.current
            const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
            return res.send({
                status: 0,
                message: "获取成功",
                data: { datalist, total }
            })
        })
        //     ///-------------------------------------------------------------------
    }
}

//获取教师信息
exports.get_teacherAllInfo = function (req, res) {
    // console.log(req.query);
    const pageSize = req.query.pageSize
    const current = req.query.current

    if (!req.query.region && !req.query.user) {
        db.query('select * from teacher order by teacher_num', function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有查到教师信息')
            const total = results.length
            const datalist = results.slice((current - 1) * pageSize, current * pageSize)
            res.send({
                status: 0,
                message: '获取教师信息成功',
                data: { datalist, total }
            })
        })
    }
    if (req.query.region && !req.query.user) {
        db.query('select * from teacher where teacher_college=? order by teacher_num', req.query.region, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有查到教师信息')
            const total = results.length
            const datalist = results.slice((current - 1) * pageSize, current * pageSize)
            res.send({
                status: 0,
                message: '获取教师信息成功',
                data: { datalist, total }
            })
        })
    }
    if (!req.query.region && req.query.user) {
        db.query('select * from teacher where teacher_num=? order by teacher_num', req.query.user, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有查到教师信息')
            const total = results.length
            const datalist = results.slice((current - 1) * pageSize, current * pageSize)
            res.send({
                status: 0,
                message: '获取教师信息成功',
                data: { datalist, total }
            })
        })
    }
    if (req.query.region && req.query.user) {
        db.query('select * from teacher where teacher_num=? and teacher_college=? order by teacher_num', [req.query.user, req.query.region], function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有查到教师信息')
            const total = results.length
            const datalist = results.slice((current - 1) * pageSize, current * pageSize)
            res.send({
                status: 0,
                message: '获取教师信息成功',
                data: { datalist, total }
            })
        })
    }
}

//获取所有学生的毕业设计情况
exports.get_allPaper = function (req, res) {

    const current = req.query.current
    const pageSize = req.query.pageSize
    const arr = []
    if (!req.query.region && !req.query.user) {
        //查询所有学生信息
        db.query('select * from student', function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有学生信息')
            // console.log(results);
            results.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);

            //查询学生的选题
            db.query('select * from topic where student_num in (?)', [arr], function (err, result) {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('没有学生选题')
                // console.log(result);

                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?) order by article_studentId', [arr], function (err, resu) {
                    if (err) return res.cc(err)
                    if (resu.length < 1) return res.cc('还没有学生上传终稿')
                    resu.forEach(value => {
                        result.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { topic_name: val.topic_name })
                        })
                    })
                    resu.forEach(value => {
                        results.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { student_name: val.student_name, student_college: val.student_college })
                        })
                    })

                    resu.forEach(value => {
                        if (value.article_teacherscore && value.look_teacherScore && value.reply_score) {
                            Object.assign(value, { allScore: value.article_teacherscore * 0.3 + value.look_teacherScore * 0.3 + value.reply_score * 0.4 })
                        } else {
                            Object.assign(value, { allScore: '部分成绩没有' })
                        }
                    })
                    resu.forEach(value => {
                        if (value.reply_status == 0) {
                            Object.assign(value, { status: '未通过' })
                        } else {
                            Object.assign(value, { status: '通过' })
                        }
                    })

                    const total = resu.length
                    const datalist = resu.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '毕设通过的人',
                        data: { datalist, total }
                    })

                })


            })
        })
    }

    if (req.query.region && !req.query.user) {
        //查询所有学生信息
        db.query('select * from student where student_college=?', req.query.region, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有学生信息')
            // console.log(results);
            results.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);
            //查询学生的选题
            db.query('select * from topic where student_num in (?)', [arr], function (err, result) {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('没有学生选题')
                // console.log(result);
                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?)', [arr], function (err, resu) {
                    if (err) return res.cc(err)
                    if (resu.length < 1) return res.cc('还没有学生通过开题答辩')
                    resu.forEach(value => {
                        result.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { topic_name: val.topic_name })
                        })
                    })
                    resu.forEach(value => {
                        results.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { student_name: val.student_name, student_college: val.student_college })
                        })
                    })
                    resu.forEach(value => {
                        if (value.article_teacherscore && value.look_teacherScore && value.reply_score) {
                            Object.assign(value, { allScore: value.article_teacherscore * 0.3 + value.look_teacherScore * 0.3 + value.reply_score * 0.4 })
                        } else {
                            Object.assign(value, { allScore: '部分成绩没有' })
                        }
                    })
                    resu.forEach(value => {
                        if (value.reply_status == 0) {
                            Object.assign(value, { status: '未通过' })
                        } else {
                            Object.assign(value, { status: '通过' })
                        }
                    })
                    const total = resu.length
                    const datalist = resu.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '毕设通过的人',
                        data: { datalist, total }
                    })
                })
            })
        })
    }


    if (!req.query.region && req.query.user) {
        //查询所有学生信息
        db.query('select * from student where student_num=?', req.query.user, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有学生信息')
            // console.log(results);
            results.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);
            //查询学生的选题
            db.query('select * from topic where student_num in (?)', [arr], function (err, result) {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('没有学生选题')
                // console.log(result);
                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?)', [arr], function (err, resu) {
                    if (err) return res.cc(err)
                    if (resu.length < 1) return res.cc('还没有学生通过开题答辩')
                    resu.forEach(value => {
                        result.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { topic_name: val.topic_name })
                        })
                    })
                    resu.forEach(value => {
                        results.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { student_name: val.student_name, student_college: val.student_college })
                        })
                    })
                    resu.forEach(value => {
                        if (value.article_teacherscore && value.look_teacherScore && value.reply_score) {
                            Object.assign(value, { allScore: value.article_teacherscore * 0.3 + value.look_teacherScore * 0.3 + value.reply_score * 0.4 })
                        } else {
                            Object.assign(value, { allScore: '部分成绩没有' })
                        }
                    })
                    resu.forEach(value => {
                        if (value.reply_status == 0) {
                            Object.assign(value, { status: '未通过' })
                        } else {
                            Object.assign(value, { status: '通过' })
                        }
                    })
                    const total = resu.length
                    const datalist = resu.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '毕设通过的人',
                        data: { datalist, total }
                    })
                })
            })
        })
    }

    if (req.query.region && req.query.user) {
        //查询所有学生信息
        db.query('select * from student where student_num=?', req.query.user, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有学生信息')
            // console.log(results);
            results.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);
            //查询学生的选题
            db.query('select * from topic where student_num in (?)', [arr], function (err, result) {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('没有学生选题')
                // console.log(result);
                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?)', [arr], function (err, resu) {
                    if (err) return res.cc(err)
                    if (resu.length < 1) return res.cc('还没有学生通过开题答辩')
                    resu.forEach(value => {
                        result.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { topic_name: val.topic_name })
                        })
                    })
                    resu.forEach(value => {
                        results.forEach(val => {
                            if (val.student_num == value.article_studentId)
                                Object.assign(value, { student_name: val.student_name, student_college: val.student_college })
                        })
                    })
                    resu.forEach(value => {
                        if (value.article_teacherscore && value.look_teacherScore && value.reply_score) {
                            Object.assign(value, { allScore: value.article_teacherscore * 0.3 + value.look_teacherScore * 0.3 + value.reply_score * 0.4 })
                        } else {
                            Object.assign(value, { allScore: '部分成绩没有' })
                        }
                    })
                    resu.forEach(value => {
                        if (value.reply_status == 0) {
                            Object.assign(value, { status: '未通过' })
                        } else {
                            Object.assign(value, { status: '通过' })
                        }
                    })
                    const total = resu.length
                    const datalist = resu.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '毕设通过的人',
                        data: { datalist, total }
                    })
                })
            })
        })
    }

}

//修改个人的信息
exports.update_info = function (req, res) {
    // console.log(req.user);
    if (req.body.phone && req.body.email) {
        db.query('update bigadmin_info set ? where num=?', [{ phone: req.body.phone, email: req.body.email }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败1')
            res.send({
                status: 0,
                message: '修改成功',
            })
        })
    }
    if (!req.body.phone && req.body.email) {
        db.query('update bigadmin_info set ? where num=?', [{ email: req.body.email }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败2')
            res.send({
                status: 0,
                message: '修改成功',
            })
        })
    }
    if (req.body.phone && !req.body.email) {
        db.query('update bigadmin_info set ? where num=?', [{ phone: req.body.phone }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败3')
            res.send({
                status: 0,
                message: '修改成功',
            })
        })
    }
    if (!req.body.phone && !req.body.email) {
        res.cc('请输入修改的信息')
    }
}

//修改密码
exports.update_pwd = function (req, res) {
    if (!req.body.old) return res.cc('请输入旧密码')
    if (!req.body.pass) {
        return res.cc('没有输入新密码')
    } else {
        db.query('select * from bigadmin where username=?', req.user.username, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('查询失败')
            if (req.body.old != results[0].password) {
                return res.cc('原密码不正确')
            } else {
                db.query('update bigadmin set ? where username=?', [{ password: req.body.checkPass }, req.user.username], function (err, result) {
                    if (err) return res.cc(err)
                    if (results.length < 1) return res.cc('修改失败')
                    res.send({
                        status: 0,
                        message: '修改成功'
                    })
                })
            }
        })
    }

}

//获取自己发布的公告
exports.get_notice = function (req, res) {
    // console.log(req.user);
    db.query('select * from notice where dele_status=0 and madmin_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有发布公告')
        res.send({
            status: 0,
            message: '公告来了',
            data: results
        })
    })
}

//获取教师提交的毕设题
exports.teacher_topic = function (req, res) {
    const arr = []
    db.query('select * from teacher', function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('获取失败')
        // console.log(resul);
        resul.forEach(val => {
            arr.push(val.teacher_num)
        })
        db.query('select * from topic where teacher_id in (?) and pass=0', [arr], function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有新提交的信息')
            res.send({
                status: 0,
                message: '教师的课题',
                data: results
            })
        })
    })
}

//通过教师提交的毕设题
exports.pass_topic = function (req, res) {
    // console.log(req.body);
    db.query('update topic set pass=1 where id=?', req.body.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('修改失败')
        res.send({
            status: 0,
            message: '通过成功',
        })
    })
}

//添加评阅小组
exports.insert_group = function (req, res) {
    // console.log(req.body);
    const obj = {
        group_leader: req.body.region,
        group_num: req.body.user,
        group_collage: req.body.teacher_college,
        group_style: req.body.style,
        group_address: req.body.address,
        teacher_num: req.body.teacher_num,
        time: req.body.value1
    }
    // console.log(results);
    db.query('select * from teacher', function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('获取失败')
        const arr = []
        resul.forEach(val => {
            arr.push(val.teacher_num)
        })
        db.query('select * from teacher_group where teacher_num in (?)', [arr], function (err, resu) {
            if (err) return res.cc(err)
            // console.log(resu);
            if (resu.length < 1) {
                return db.query('insert into teacher_group set ?', obj, function (err, result) {
                    if (err) return res.cc(err)
                    if (result.affectedRow < 1) return res.cc('插入失败')
                    res.send({
                        status: 0,
                        message: '分配成功',
                    })
                })
            }
            if (resu.length > 0) {
                // console.log(req.body.style);
                const fil = resu.filter(val => {
                    return val.teacher_num == req.body.teacher_num
                })
                // console.log(fil);
                const booler = fil.some(val => {
                    // console.log(val.group_style);
                    return val.group_style == req.body.style
                })
                // console.log(booler);
                if (booler) {
                    res.send({
                        status: 0,
                        message: '该类型已经被添加',
                    })
                }
                if (!booler) {
                    // console.log(req.body.style);
                    const filt = resu.filter(value => {
                        return value.group_style == req.body.style && value.group_collage == req.body.teacher_college
                    })
                    // console.log(filt);

                    const boolea = filt.filter(value => {
                        return value.group_num == req.body.user
                    })
                    // console.log(boolea);
                    if (boolea) {
                        const bool = boolea.some(val => {
                            return val.group_leader == '是'
                        })
                        if (!bool && req.body.region == '是' || req.body.region == '否') {
                            db.query('insert into teacher_group set ?', obj, function (err, re) {
                                if (err) return res.cc(err)
                                if (re.affectedRow < 1) return res.cc('插入失败')
                                res.send({
                                    status: 0,
                                    message: '分配成功',
                                })
                            })
                        } else {
                            res.send({
                                status: 0,
                                message: '该小组有组长了',
                            })
                        }
                    }
                }

            }

        })
    })
}

//获取所有学院教师信息
exports.get_collageteacher = function (req, res) {
    db.query('select * from teacher ', function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: result
        })
    })
}

//获取答辩小组组长
exports.get_groupTeacher = function (req, res) {
    // console.log(req.user);
    db.query('select * from teacher', function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('没有查到教师')
        // console.log(resul);
        db.query('select * from teacher_group where group_collage=? and group_leader="是" and group_style="开题答辩小组"', req.body.region, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有开题答辩的教师')
            // console.log(results);
            resul.forEach(val => {
                results.forEach(value => {
                    if (val.teacher_num == value.teacher_num) {
                        Object.assign(value, { teacher_name: val.teacher_name })
                    }
                })
            })
            res.send({
                status: 0,
                message: '小组组长来了',
                data: results
            })
        })
    })
}

//获取毕业答辩小组组长
exports.groupteacher = function (req, res) {
    // console.log(req.body);
    db.query('select * from teacher', function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('该学院没有查到教师')
        db.query('select * from teacher_group where group_collage=? and  group_leader="是" and group_style="毕业论文答辩小组"', req.body.region, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有毕业答辩的教师')
            resul.forEach(val => {
                results.forEach(value => {
                    if (val.teacher_num == value.teacher_num) {
                        Object.assign(value, { teacher_name: val.teacher_name })
                    }
                })
            })
            // console.log(results);
            res.send({
                status: 0,
                message: '小组组长来了',
                data: results
            })
        })
    })
}

//获取能够参与开题报告答辩学生信息
exports.get_studentreportinfo = function (req, res) {
    // console.log(req.user[0]);
    db.query('select * from report where report_status=1', function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('还没有学生通过教师')
        // console.log(resul);
        const arr = []
        resul.forEach(val => {
            arr.push(val.student_num)
        })
        // console.log(arr);
        db.query('select * from student where student_num in (?) and student_college=?', [arr, req.body.group_collage], function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('获取失败')
            // console.log(results);
            res.send({
                status: 0,
                message: '获取成功',
                data: results
            })
        })
    })
}

//获取已经分配的开题答辩的学生信息
exports.distribution_studentinfo = function (req, res) {
    db.query('select * from student_group where collage=? and group_style="开题答辩小组"', req.body.group_collage, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: result
        })
    })
}


//获取能够参与论文答辩学生信息
exports.get_studentPaperinfo = function (req, res) {
    db.query('select * from report where reply_status=1', function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('还没有学生通过答辩小组')
        const arr = []
        resul.forEach(val => {
            arr.push(val.student_num)
        })
        // console.log(arr);
        db.query('select * from student where student_num in (?) and student_college=?', [arr, req.body.group_collage], function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('获取失败')
            // console.log(results);
            res.send({
                status: 0,
                message: '获取成功',
                data: results
            })
        })

    })
}

//获取已经毕业答辩分配的学生信息
exports.distribution_paperinfo = function (req, res) {
    db.query('select * from student_group where collage=? and group_style="毕业论文答辩小组"', req.body.group_collage, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: result
        })
    })
}


//获取能够分配评阅教师的学生
exports.lookTeacher_student = function (req, res) {
    const arr = []
    const arr1 = []
    const arr2 = []
    const arr3 = []
    db.query('select * from report where reply_status=1', function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('还没有学生选题')
        results.forEach(val => {
            arr.push(val.student_num)
        })
        // console.log(arr);
        db.query('select * from student where student_college=? and student_num in (?)', [req.body.region, arr], function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('还没有学生')
            // console.log(result);
            result.forEach(val => {
                arr1.push(val.student_num)
            })
            // console.log(arr1);
            db.query('select * from other_teacher_student where student_num in (?)', [arr1], function (err, resul) {
                if (err) return res.cc(err)
                if (resul.length < 1) {
                    db.query('select * from teacher_student where studentId in (?)', [arr1], function (err, resultss) {
                        if (err) return res.cc(err)
                        if (resultss.length < 1) return res.cc('查询失败')
                        // console.log(resultss);
                        resultss.forEach(val => {
                            arr2.push(val.teacherId)
                        })
                        // console.log(arr2);
                        db.query('select * from teacher where teacher_college=? and teacher_num in (?)', [req.body.region, arr2], function (err, resu) {
                            if (err) return res.cc(err)
                            if (resu.length < 1) return res.cc('还没有教师')
                            // console.log(resu);
                            result.forEach(value => {
                                resultss.forEach(val => {
                                    if (value.student_num == val.studentId) {
                                        Object.assign(value, { teacher_num: val.teacherId })
                                    }
                                })
                            })
                            // console.log(result);
                            result.forEach(value => {
                                resu.forEach(val => {
                                    if (value.teacher_num == val.teacher_num) {
                                        Object.assign(value, { teacher_name: val.teacher_name })
                                    }
                                })
                            })
                            res.send({
                                status: 0,
                                message: '查到了1',
                                data: result
                            })
                        })
                    })
                }
                else {
                    resul.forEach(val => {
                        arr3.push(val.teacher_num)
                    })
                    db.query('select * from teacher_student where studentId in (?)', [arr1], function (err, resultss) {
                        if (err) return res.cc(err)
                        if (resultss.length < 1) return res.cc('查询失败')
                        // console.log(resultss);
                        resultss.forEach(val => {
                            arr2.push(val.teacherId)
                        })
                        // console.log(arr2);
                        db.query('select * from teacher where teacher_college=? and teacher_num in (?)', [req.body.region, arr2], function (err, resu) {
                            if (err) return res.cc(err)
                            if (resu.length < 1) return res.cc('还没有教师')
                            result.forEach(value => {
                                resultss.forEach(val => {
                                    if (value.student_num == val.studentId) {
                                        Object.assign(value, { teacher_num: val.teacherId })
                                    }
                                })
                            })
                            // console.log(result);
                            result.forEach(value => {
                                resu.forEach(val => {
                                    if (value.teacher_num == val.teacher_num) {
                                        Object.assign(value, { teacher_name: val.teacher_name })
                                    }
                                })
                            })
                            // console.log(arr3);
                            db.query('select * from teacher where teacher_college=? and teacher_num in (?)', [req.body.region, arr3], function (err, re) {
                                if (err) return res.cc(err)
                                if (resu.length < 1) return res.cc('没有评阅教师信息')
                                // console.log(re);
                                result.forEach(value => {
                                    resultss.forEach(val => {
                                        if (value.student_num == val.studentId) {
                                            Object.assign(value, { teacher_num: val.teacherId })
                                        }
                                    })
                                })
                                // console.log(result);
                                result.forEach(value => {
                                    resu.forEach(val => {
                                        if (value.teacher_num == val.teacher_num) {
                                            Object.assign(value, { teacher_name: val.teacher_name })
                                        }
                                    })
                                })
                                result.forEach(value => {
                                    resul.forEach(val => {
                                        if (value.student_num == val.student_num) {
                                            Object.assign(value, { lookTeacher_num: val.teacher_num })
                                        }
                                    })
                                })
                                result.forEach(value => {
                                    re.forEach(val => {
                                        if (value.lookTeacher_num == val.teacher_num) {
                                            Object.assign(value, { lookTeacher_name: val.teacher_name })
                                        }
                                    })
                                })
                                // console.log(result);
                                res.send({
                                    status: 0,
                                    message: '查到了2',
                                    data: result
                                })
                            })
                        })
                    })
                }
            })
        })
    })
}

//获取所有学院教师信息
exports.get_collageteachers = function (req, res) {
    // console.log(req.body.region);
    db.query('select * from teacher where teacher_college=?', req.body.region, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: result
        })
    })
}

// 添加评阅教师
exports.insert_lookTeacher = function (req, res) {
    // console.log(req.body);
    db.query('select * from other_teacher_student where student_num=?', req.body.student_num, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1)
            return db.query('insert into other_teacher_student set ? ', { student_num: req.body.student_num, teacher_num: req.body.teacher_num }, function (err, results) {
                if (err) return res.cc(err)
                if (results.affectedRow < 1) return res.cc('插入失败')
                res.send({
                    status: 0,
                    message: '插入成功'
                })
            })
        return res.send({
            status: 0,
            message: '你已经选择了'
        })
    })
}

//获取二级学院管理员的信息
exports.secondaryInfo = function (req, res) {
    db.query('select * from minadmin_info where collage=? order by num', req.body.region, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有二级学院管理信息的信息')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//删除二级管理员
exports.dele_secondaryInfo = function (req, res) {
    // console.log(req.body);
    db.query('delete from minadmin_info where num=?', req.body.num, function (err, result) {
        if (err) return res.cc(err)
        if (result.affectedRow < 1) return res.cc('删除失败')
        db.query('delete from minadmin where username=?', req.body.num, function (err, result) {
            if (err) return res.cc(err)
            if (result.affectedRow < 1) return res.cc('删除失败')
            res.send({
                status: 0,
                message: '删除成功'
            })
        })
    })
}

//获取二级管理员旧密码
exports.get_secondaryInfopwd = function (req, res) {
    // console.log(req.body);
    db.query('select * from minadmin where username=?', req.body.num, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: result
        })
    })
}

//修改二级管理员密码
exports.update_secondaryInfopwd = function (req, res) {
    // console.log(req.body);
    db.query('update minadmin set password=? where password=?', [req.body.new, req.body.old], function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('修改失败')
        res.send({
            status: 0,
            message: '修改成功',
            data: result
        })
    })
}

//添加二级管理员
exports.add_secondaryInfo = function (req, res) {
    // console.log(req.body);
    db.query('select * from minadmin_info where num=?', req.body.num, function (err, result) {
        if (err) return res.cc(err)
        if (result.length > 0) return res.cc('账号存在')
        db.query('insert into minadmin_info set ?', { name: req.body.name, num: req.body.num, collage: req.body.region }, function (err, result) {
            if (err) return res.cc(err)
            if (result.affectedRow < 1) return res.cc('添加失败')
            db.query('insert into minadmin set ?', { username: req.body.num, password: req.body.num }, function (err, result) {
                if (err) return res.cc(err)
                if (result.affectedRow < 1) return res.cc('添加失败')
                res.send({
                    status: 0,
                    message: '添加成功'
                })
            })
        })
    })
}

//查看管理员
exports.see_secondaryInfo = function (req, res) {
    db.query('select * from minadmin_info where num=?', req.body.num, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有信息')
        res.send({
            status: 0,
            message: '信息来了',
            data: results
        })
    })
}


//删除学生信息
exports.dele_studentinfo = function (req, res) {
    // console.log(req.body);
    db.query('delete from user where username=?', req.body.student_num, function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRow < 1) return res.cc('删除登录表的信息失败')
        db.query('delete from student where student_num=?', req.body.student_num, function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('删除学生表的信息失败')
            db.query('update  topic set ? where student_num=?', [{ topic_status: 0, student_num: null }, req.body.student_num], function (err, results) {
                if (err) return res.cc(err)
                if (results.affectedRow < 1) return res.cc('删除课题表的信息失败')
                db.query('delete from task where student_num=?', req.body.student_num, function (err, results) {
                    if (err) return res.cc(err)
                    if (results.affectedRow < 1) return res.cc('删除任务书表的信息失败')
                    db.query('delete from student_group where student_num=?', req.body.student_num, function (err, results) {
                        if (err) return res.cc(err)
                        if (results.affectedRow < 1) return res.cc('删除学生分组的信息失败')
                        db.query('delete from respondmessage where student_num=?', req.body.student_num, function (err, results) {
                            if (err) return res.cc(err)
                            if (results.affectedRow < 1) return res.cc('删除留言回复表的信息失败')
                            db.query('delete from other_teacher_student where student_num=?', req.body.student_num, function (err, results) {
                                if (err) return res.cc(err)
                                if (results.affectedRow < 1) return res.cc('删除评阅教师分组的信息失败')
                                db.query('delete from report where student_num=?', req.body.student_num, function (err, results) {
                                    if (err) return res.cc(err)
                                    if (results.affectedRow < 1) return res.cc('删除留言回复表的信息失败')
                                    db.query('delete from message where student_num=?', req.body.student_num, function (err, results) {
                                        if (err) return res.cc(err)
                                        if (results.affectedRow < 1) return res.cc('删除留言信息失败')
                                        db.query('delete from direct where student_num=?', req.body.student_num, function (err, results) {
                                            if (err) return res.cc(err)
                                            if (results.affectedRow < 1) return res.cc('删除指导记录信息失败')
                                            db.query('delete from article where article_studentId=?', req.body.student_num, function (err, results) {
                                                if (err) return res.cc(err)
                                                if (results.affectedRow < 1) return res.cc('删除指导记录信息失败')
                                                res.send({
                                                    status: 0,
                                                    message: '删除成功'
                                                })
                                            })
                                        })
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })
    })
}

//添加学生信息
exports.add_studentInfo = function (req, res) {
    // console.log(req.body);
    db.query('select * from student where student_num=?', req.body.num, function (err, result) {
        if (err) return res.cc(err)
        if (result.length > 0) return res.cc('账号存在')
        db.query('insert into student set ?', { student_class: req.body.class, student_name: req.body.name, student_num: req.body.num, student_college: req.body.region }, function (err, result) {
            if (err) return res.cc(err)
            if (result.affectedRow < 1) return res.cc('添加失败')
            db.query('insert into user set ?', { username: req.body.num, password: req.body.num }, function (err, result) {
                if (err) return res.cc(err)
                if (result.affectedRow < 1) return res.cc('添加失败')
                res.send({
                    status: 0,
                    message: '添加成功'
                })
            })
        })
    })
}


//添加教师信息
exports.add_teacherInfo = function (req, res) {
    // console.log(req.body);
    db.query('select * from teacher where teacher_num=?', req.body.num, function (err, result) {
        if (err) return res.cc(err)
        if (result.length > 0) return res.cc('账号存在')
        db.query('insert into teacher set ?', { teacher_degree: req.body.degree, teacher_Workplace: req.body.workplace, teacher_name: req.body.name, teacher_num: req.body.num, teacher_college: req.body.region }, function (err, result) {
            if (err) return res.cc(err)
            if (result.affectedRow < 1) return res.cc('添加失败')
            db.query('insert into teacher_login set ?', { username: req.body.num, password: req.body.num }, function (err, result) {
                if (err) return res.cc(err)
                if (result.affectedRow < 1) return res.cc('添加失败')
                res.send({
                    status: 0,
                    message: '添加成功'
                })
            })
        })
    })
}

//删除教师信息
exports.dele_teacherinfo = function (req, res) {
    // console.log(req.body);
    db.query('delete from teacher_login where username=?', req.body.teacher_num, function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRow < 1) return res.cc('删除登录表的信息失败')
        db.query('delete from teacher where teacher_num=?', req.body.teacher_num, function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('删除教师表的信息失败')
            db.query('delete from topic where teacher_id=?', req.body.teacher_num, function (err, results) {
                if (err) return res.cc(err)
                db.query('delete from task where teacher_num=?', req.body.teacher_num, function (err, results) {
                    if (err) return res.cc(err)
                    db.query('delete from teacher_group where teacher_num=?', req.body.teacher_num, function (err, results) {
                        if (err) return res.cc(err)
                        db.query('delete from respondmessage where teacher_num=?', req.body.teacher_num, function (err, results) {
                            if (err) return res.cc(err)
                            db.query('delete from other_teacher_student where teacher_num=?', req.body.teacher_num, function (err, results) {
                                if (err) return res.cc(err)
                                db.query('delete from report where teacher_num=?', req.body.teacher_num, function (err, results) {
                                    if (err) return res.cc(err)
                                    db.query('delete from direct where teacher_num=?', req.body.teacher_num, function (err, results) {
                                        if (err) return res.cc(err)
                                        res.send({
                                            status: 0,
                                            message: '删除成功'
                                        })
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })
    })
}

// 导入学生文件信息
exports.insert_file = function (req, res) {
    for (let item in req.body) {
        // console.log(req.body[item]);
        let con = {
            student_name: req.body[item].student_name,
            student_num: req.body[item].student_num,
            student_class: req.body[item].student_class,
            student_college: req.body[item].college_name,
        }
        db.query('insert into student set ?', con, function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 0) return res.cc('添加失败')
        })

    }
    res.send({
        message: "ok"
    })
}
